package com.sczmgk.track.view;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.text.method.TransformationMethod;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView.OnEditorActionListener;

import com.sczmgk.track.R;
import com.sczmgk.track.utils.CommonUtils;


/**
 * 自定义带删除按钮的Edittext
 * <p/>
 * 1.设置输入类型
 * 2.设置是否显示密码
 * 3.设置提示文字
 * 4.设置输入文字的最大长度
 *
 * @author bo
 */
public class CustomDeleteInfoView extends LinearLayout implements View.OnClickListener {
    private String TAG = CustomDeleteInfoView.class.getSimpleName();


    public CustomDeleteInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomDeleteInfoView);

        CharSequence hint = null;
        Integer hintColor = -1;
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.CustomDeleteInfoView_hint:
                    hint = ta.getText(attr);
                    break;
                case R.styleable.CustomDeleteInfoView_hintColor:

                    break;
            }
        }
        if (hint != null) setHint(hint);

        if (hintColor != -1) {//没有起到作用
            setHintColor(hintColor);
        }
    }

    public CustomDeleteInfoView(Context context) {
        super(context);
        init();
    }

    /**
     * 输入文字的控件
     */
    private EditText inputInfo;
    /**
     * 删除内容的图片
     */
    private ImageView delete;
    /**
     * 暂时没有用到
     */
    private OnClickListener deleteClickListener;

    private void setHintColor(int hintColor) {
        inputInfo.setHintTextColor(hintColor);
    }

    private void init() {
        LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        View view = View.inflate(getContext(), R.layout.delete_info_layout, null);
        view.setLayoutParams(layoutParams);

        inputInfo = (EditText) view.findViewById(R.id.et_input_info);
        delete = (ImageView) view.findViewById(R.id.delete_icon);
        addTextChangedListener();
//		MyLog.i(TAG,"edittext 是否得到焦点："+inputInfo.isFocused());
        if (!isEmpty(inputInfo.getText()) && inputInfo.isFocused()) {
//			 MyLog.d(TAG,"显示叉");
            delete.setVisibility(View.VISIBLE);
            if (onDelectPicShowed != null) {
                onDelectPicShowed.onShow();
            }
        } else {
//		    	 MyLog.e(TAG,"--不显示叉");

            delete.setVisibility(View.INVISIBLE);
            if (onDelectPicShowed != null) {
                onDelectPicShowed.onHide();
            }
        }
        delete.setOnClickListener(this);
        addView(view);

    }

    /**
     * @param gravity 如：Gravity.RIGHT
     */
    public void setGravity(int gravity) {
        inputInfo.setGravity(Gravity.RIGHT);
    }

    /**
     * 设置光标的位置
     *
     * @param index
     */
    public void setSelection(int index) {
        inputInfo.setSelection(index);
    }

    /**
     * EditText  文本的长度
     *
     * @return
     */
    public int length() {
        return inputInfo.length();
    }

    /**
     * 设置编辑动作的监听
     *
     * @param onEditorActionListener
     */
    public void setOnEditorActionListener(OnEditorActionListener onEditorActionListener) {
        inputInfo.setOnEditorActionListener(onEditorActionListener);
        inputInfo.length();
    }

    /**
     * 设置输入选项   EditorInfo.IME_ACTION_SEARCH;（设置为搜索）
     *
     * @param imeOptions
     */
    public void setImeOptions(int imeOptions) {
        inputInfo.setImeOptions(imeOptions);
    }

    /**
     * 设置文字的大小（单位为sp）
     *
     * @param size
     */
    public void setTextSize(float size) {
        inputInfo.setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    /**
     * 设置文字输入的最大长度
     *
     * @param length
     */
    public void setMaxLength(int length) {
        inputInfo.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
    }

    /**
     * 设置单行显示
     */
    public void setSingleLine() {
        inputInfo.setSingleLine();
    }

    /**
     * 取得文本
     *
     * @return
     */
    public String getText() {
        return inputInfo.getText().toString().trim();
    }

    public EditText getEditText() {
        return inputInfo;
    }


    /**
     * 设置文本
     *
     * @param text
     */
    public void setText(String text) {
        inputInfo.setText(text);
    }

    /**
     * 设置输入的类型
     *
     * @param type EditorInfo.TYPE_CLASS_PHONE//如输入电话号码
     */
    public void setInputType(int type) {
        inputInfo.setInputType(type);
    }

    /**
     * 设置提示文字
     *
     * @param hint
     */
    public void setHint(int hint) {
        inputInfo.setHint(hint);
    }

    /**
     * 设置提示文字
     *
     * @param hint
     */
    public void setHint(CharSequence hint) {
        inputInfo.setHint(hint);
    }

    /**
     * 设置是否显示密码
     *
     * @param method 如不显示密码  PasswordTransformationMethod.getInstance()
     */
    public void setTransformationMethod(TransformationMethod method) {
        inputInfo.setTransformationMethod(method);
    }

    /**
     * 设置文本变化的监听
     */
    private void addTextChangedListener() {
        addTextChangedListener(null);
    }

    public void setFilters(InputFilter[] filters) {
        inputInfo.setFilters(filters);
    }

    public void setTextChangedListener(TextWatcher textWatcher) {
        addTextChangedListener(null);

    }

    /**
     * 设置文本变化的监听
     */
    public void addTextChangedListener(final OnAfterTextChangedListener afterTextChangedListener) {
        inputInfo.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (inputInfo.getText().length() == 0) {
                    delete.setVisibility(View.INVISIBLE);
                    if (onDelectPicShowed != null) {
                        onDelectPicShowed.onHide();
                    }
                } else {
                    if (onDelectPicShowed != null) {
                        onDelectPicShowed.onShow();
                    }
                    if (inputInfo.isFocused()) {
                        delete.setVisibility(View.VISIBLE);

                    }
                }
                if (afterTextChangedListener != null) {
                    afterTextChangedListener.onTextChanged();
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (afterTextChangedListener != null) {
                    afterTextChangedListener.onAfterTextChanged();
                }
            }
        });
        inputInfo.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    delete.setVisibility(View.INVISIBLE);
                    if (onDelectPicShowed != null) {
                        onDelectPicShowed.onHide();
                    }
                } else {
                    if (!isEmpty(inputInfo.getText().toString())) {
                        delete.setVisibility(View.VISIBLE);
                        if (onDelectPicShowed != null) {
                            onDelectPicShowed.onShow();
                        }
                    }
                }

            }
        });
    }

    /**
     * @param listener
     */
    public void setDeleteClickListener(OnClickListener listener) {
        deleteClickListener = listener;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.delete_icon:
                if (deleteClickListener != null) {
                    deleteClickListener.onClick(v);//外面调用的Click
                }
                inputInfo.setText("");
                break;
            default:
                break;
        }
    }

    public static boolean isEmpty(CharSequence str) {
        if (str == null || str.length() == 0)
            return true;
        else
            return false;
    }

    /**
     * 展示软键盘
     */
    public void showSoftInputFromWindow(Activity context, EditText editText) {
        CommonUtils.showSoftInputFromWindow(context,editText);
    }

    /**
     * 输入框文本变化后的监听接口
     *
     * @author bo
     */
    public interface OnAfterTextChangedListener {
        void onAfterTextChanged();

        void onTextChanged();
    }

    public void setOnDelectPicShowed(OnDelectPicShowed onDelectPicShowed) {
        this.onDelectPicShowed = onDelectPicShowed;
    }

    OnDelectPicShowed onDelectPicShowed;

    public interface OnDelectPicShowed {
        void onShow();

        void onHide();
    }

    public void setOnFocusChangeListener(OnFocusChangeListener onFocusChangeListener) {
        inputInfo.setOnFocusChangeListener(onFocusChangeListener);
    }

    public void setFocusable(boolean focusable) {
        inputInfo.setFocusable(focusable);
        inputInfo.requestFocus();
    }

    public void clearFocus() {
        inputInfo.clearFocus();
    }

    public void setFocusableInTouchMode(boolean focusableInTouchMode) {
        inputInfo.setFocusableInTouchMode(focusableInTouchMode);
    }
}
